package top.z.work.module.system.controller.role;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.z.work.framework.common.pojo.PageInfo;
import top.z.work.framework.common.pojo.R;
import top.z.work.module.system.controller.role.vo.*;
import top.z.work.module.system.controller.user.vo.SystemUserVO;
import top.z.work.module.system.convert.SystemRoleConvert;
import top.z.work.module.system.convert.SystemUserConvert;
import top.z.work.module.system.entity.SystemRole;
import top.z.work.module.system.entity.SystemUser;
import top.z.work.module.system.service.SystemRoleService;

import java.util.List;

/**
 * 角色管理Controller
 *
 * @author zaoangod
 * @since 2024-05-10 16:38:44
 */
@Tag(name = "角色管理")
@Slf4j
@Validated
@RestController
@RequestMapping("system/role")
public class SystemRoleController {

    @Resource
    private SystemRoleService systemRoleService;

    @Operation(summary = "角色分页", description = "获取角色分页")
    @GetMapping("page")
    public R<PageInfo<SystemRoleVO>> list(
            SystemRoleVO parameter,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "size", defaultValue = "10") Integer size
    ) {
        QueryWrapper     query    = SystemRoleConvert.INSTANCE.convert(parameter);
        Page<SystemRole> data     = Page.of(page, size);
        Page<SystemRole> pageInfo = systemRoleService.page(data, query);
        return R.s(SystemRoleConvert.INSTANCE.convert(pageInfo));
    }

    @Operation(summary = "角色列表", description = "获取角色列表")
    @GetMapping("list")
    public R<List<SystemRoleVO>> queryList(SystemRoleVO parameter) {
        QueryWrapper     query = SystemRoleConvert.INSTANCE.convert(parameter);
        List<SystemRole> list  = systemRoleService.list(query);
        return R.s(SystemRoleConvert.INSTANCE.convert(list));
    }

    @Operation(summary = "角色信息", description = "获取角色信息")
    @GetMapping("info")
    public R<SystemRoleVO> queryOne(SystemRoleVO parameter) {
        QueryWrapper query      = SystemRoleConvert.INSTANCE.convert(parameter);
        SystemRole   SystemRole = systemRoleService.getOne(query);
        return R.s(SystemRoleConvert.INSTANCE.convert(SystemRole));
    }

    @Operation(summary = "删除角色", description = "删除角色数据")
    @DeleteMapping("remove")
    public R<Boolean> remove(@RequestParam("identity") @NotNull(message = "数据主键不能为空") Long identity) {
        systemRoleService.removeRole(identity);
        return R.s(true);
    }

    @Operation(summary = "修改角色", description = "修改角色数据")
    @PutMapping("modify")
    public R<SystemRole> modify(@RequestBody SystemRoleModifyVO parameter) {
        SystemRole dept = SystemRoleConvert.INSTANCE.convert(parameter);
        systemRoleService.modifyRole(dept);
        return R.s(dept);
    }

    @Operation(summary = "添加角色", description = "添加角色数据")
    @PostMapping("add")
    public R<Boolean> add(@RequestBody SystemRoleCreateVO parameter) {
        SystemRole dept    = SystemRoleConvert.INSTANCE.convert(parameter);
        boolean    success = systemRoleService.save(dept);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "批量删除", description = "批量删除角色数据")
    @PostMapping("batch-remove")
    public R<Boolean> batchRemove(
            @RequestBody
            @NotNull(message = "数据主键列表不能为空")
            @NotEmpty(message = "数据主键列表不能为空")
            List<String> identityList
    ) {
        boolean success = systemRoleService.removeByIds(identityList);
        return success ? R.s(true) : R.f();
    }

    // ---------------------------------------------------------------------

    @Operation(summary = "获取角色关联菜单", description = "获取角色关联菜单数据")
    @GetMapping("get-role-route-relate")
    public R<List<SystemRoleRelateRouteVO>> getRoleRelateRoute(@RequestParam("identity") @NotNull(message = "角色主键不能为空") Long identity) {
        return R.s(systemRoleService.getRoleRelateRoute(identity));
    }

    @Operation(summary = "添加角色关联菜单", description = "添加角色关联菜单数据")
    @PostMapping("add-role-route-relate")
    public R<Boolean> addRoleRelateRoute(@RequestBody SystemRoleRelateRouteCreateVO parameter) {
        systemRoleService.addRoleRelateRoute(parameter);
        return R.s(true);
    }

    @Operation(summary = "获取角色关联用户", description = "获取角色关联用户数据")
    @GetMapping("get-role-relate-user")
    public R<PageInfo<SystemUserVO>> getRoleRelateUser(
            @RequestParam(name = "identity") @NotNull(message = "角色主键不能为空") Long identity,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "size", defaultValue = "10") Integer size
    ) {
        Page<SystemUser> pageInfo = systemRoleService.getRoleRelateUser(identity, page, size);
        return R.s(SystemUserConvert.INSTANCE.convert(pageInfo));
    }

    @Operation(summary = "添加角色关联用户", description = "添加角色关联用户数据")
    @PostMapping("add-role-relate-user")
    public R<Boolean> addRoleRelateUser(@RequestBody SystemRoleRelateUserCreateVO parameter) {
        systemRoleService.addRoleRelateUser(parameter);
        return R.s(true);
    }

    @Operation(summary = "删除角色关联用户", description = "删除角色关联用户数据")
    @PostMapping("remove-role-relate-user")
    public R<Boolean> removeRoleRelateUser(@RequestBody SystemRoleRelateUserCreateVO parameter) {
        systemRoleService.removeRoleRelateUser(parameter);
        return R.s(true);
    }

}
